/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.partner.active;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerApplyPermissionOrder;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerApplyTechOrder;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

/**
 * <b>(ActivePartnerByLockProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/15
 */
@Component
public class ActivePartnerByLockProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 21;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        String  partnerId = switchPartnerId(serviceContext);
        Partner partner   = partnerRepository.lock(partnerId);
        Assert.notNull(partner, () -> new BusinessException(ResultCode.FAILURE, "未能锁定合作伙伴[partnerId=" + partnerId + "]"));
        partner.check();
        serviceContext.setDomain(partner);
    }

    private String switchPartnerId(ProcessorServiceContext serviceContext) {
        ServiceOrderBase order = serviceContext.getOrder();
        if (order instanceof PartnerApplyPermissionOrder || order instanceof PartnerApplyTechOrder) {
            return order.getPartnerId();
        } else {
            return null;
        }
    }
}
